package renshi.db.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
//import java.util.Date;

public class UserDao {
	DbUtil db = new DbUtil();

	public boolean addmem(int userid, String username, String passwd, String position) {
		Connection cn = db.getConn();
		String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar
				.getInstance().getTime());
		// System.out.println("-----------"+datetime);
		try {

			Statement st = cn.createStatement();

			String sql = "insert into user values('"+userid+"','" + username
					+ "','" + passwd + "','" + datetime + "','" + position+ "')";

			int result = st.executeUpdate(sql);
			// System.out.print(result);
			if (result > 0)
				return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			db.closeConn();
		}
		return false;
	}

	public boolean login(String username, String password) {

		Connection cn = db.getConn();
		try {
			Statement st = cn.createStatement();

			String sql = "select password from user where username = '"
					+ username + "'";
			// System.out.print(sql);
			ResultSet rs = st.executeQuery(sql);

			if (rs.next()) {
				if (password.equals(rs.getString("password")))
					return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeConn();
		}
		return false;
	}
	
	//-----判断登录者的身份----------------
	//根据登录帐号判断是什么身份，返回的是数据库中的字符串
	public String loginRole(String username) {
		Connection cn = db.getConn();
		try {
			Statement st = cn.createStatement();
			String sql = "select position from user where username = '"
					+ username + "'";
			// System.out.print(sql);
			ResultSet rs = st.executeQuery(sql);
			if (rs.next()) {				
					return rs.getString("position");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeConn();
		}
		return "yuangong";
	}

	public boolean isLogin(String username) {
		Connection cn = db.getConn();
		try {
			Statement st = cn.createStatement();

			String sql = "select username from user where username = '"
					+ username + "'";
			// System.out.print(sql);
			ResultSet rs = st.executeQuery(sql);

			if (rs.next()) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeConn();
		}
		return false;
	}
	
	public boolean changePasswd(String username,String newPasswd) {
		Connection cn = db.getConn();
		try {
			Statement st = cn.createStatement();

			String sql = "update user set password='"+newPasswd+"' where username = '"
					+ username + "'";
			// System.out.print(sql);
			int i = st.executeUpdate(sql);
			if (i>0) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.closeConn();
		}
		return false;
	}

}
